//
//  HWNewpingjiaView.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/9/8.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWNewpingjiaView.h"
#import "JXPagerView.h"
#import "HWpinglunFrame.h"
#import "HWpinglunHeaderView.h"
#import "HWpinglunfootView.h"
#import "HWNewCommentsTableViewCell.h"
#import "HWpinglunxiangqingshituView.h"
#import "HWLoginController.h"



@interface HWNewpingjiaView()<UITableViewDataSource,UITableViewDelegate,HWpinglunHeaderViewdelegate,HWpinglunfootViewDelegate,JXPagerViewListViewDelegate>


@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property(strong,nonatomic) UITableView *maintablview;
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(strong,nonatomic)UIImageView *shujuimagvie;
@property(strong,nonatomic)UILabel *zhawushujuLabel;

@end


@implementation HWNewpingjiaView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUI];
        //   [self detailsInterfaceCommentsxiangqing];
        
        
        
        
    }
    return self;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.maintablview.frame = self.bounds;
}


- (void)refreshData {
    
    [self detailsInterfaceCommentsxiangqing];
    
}


- (void)setUI {
    
    
    self.shujuimagvie = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-188)/2, 35, 188, 124)];
    self.shujuimagvie.image = [UIImage imageNamed:@"details_dealbg"];
    [self   addSubview: self.shujuimagvie];
    self.zhawushujuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 124+35+9, kScreenW, 12)];
    self.zhawushujuLabel.text = @"暂无评价";
    self.zhawushujuLabel.font = [UIFont systemFontOfSize:12];
    self.zhawushujuLabel.textAlignment = NSTextAlignmentCenter;
    self.zhawushujuLabel.textColor = RGB(120, 120, 120);
    [self   addSubview:self.zhawushujuLabel];
    
    
    
    // self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  1000) style:UITableViewStylePlain];
    self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,  self.frame.size.height) style:UITableViewStyleGrouped];
    self.maintablview.delegate = self;
    self.maintablview.dataSource = self;
    self.maintablview.tag = 10;
    self.maintablview.scrollEnabled = YES;
    self.maintablview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.maintablview.tableFooterView = [[UIView alloc]init];
    self.maintablview.backgroundColor = [UIColor whiteColor];
    [self  addSubview:self.maintablview];
    
    
}

// 详情评论界面
- (void)detailsInterfaceCommentsxiangqing {
    
    // 斗
    
    NSDictionary   *parametes = @{
        @"id" : self.gameId,
        @"ios" : @"1",
        @"ct" :  @"app2021",
        @"ac" : @"gameinfo_pllist"
    };
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        parametes = @{
            @"id" : self.gameId,
            @"ios" : @"1",
            @"ct" :  @"app2021",
            @"ac" : @"gameinfo_pllist"
        };
        
    } else {
        
        parametes = @{
            @"id" : self.gameId,
            @"ios" : @"1",
            @"ct" :  @"app2021",
            @"uid": [HWLocallyStoredData getLoginUserID],
            @"ac" : @"gameinfo_pllist"
        };
        
        
        
    }
    
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    
    
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            NSLog(@"%@",response);
            
            
            self.dataMuArr = [[NSMutableArray alloc] init];
            
            NSMutableArray *arr  =  [HWpinglunModel mj_objectArrayWithKeyValuesArray: response[@"data"]];
            if (arr == nil) {
                self.maintablview.hidden = YES;
                
            } else {
                
                //  self.shujuimagvie.hidden = YES ;
                self.maintablview.hidden = NO;
                
            }
            
            for (int i = 0; i < arr.count; i++) {
                
                HWpinglunFrame *mode =  [[HWpinglunFrame alloc]init];
                mode.datamodel = arr[i];
                [self.dataMuArr addObject: mode];
                
            }
            
            
            [self.maintablview reloadData];
            
            
        } else {
            
            
            [MBProgressHUD showError:response[@"message"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        
        
    }];
    
    
    
    
    
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  self.dataMuArr.count;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    HWpinglunFrame *frame =     self.dataMuArr[section];
    NSUInteger cout =    [frame.datamodel.son_list count];
    if (cout > 4) {
        
        return  4;
        
    }
    return     [frame.datamodel.son_list count];
    
    
    
    return  1;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    static NSString *cellID = @"celiD";
    HWNewCommentsTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil ) {
        cell =  [[HWNewCommentsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    HWpinglunFrame *frame =     self.dataMuArr[indexPath.section];
    HWpinglunModel *model =      frame.datamodel.son_list[indexPath.row];
    cell.frameModel = frame;
    cell.model = model ;
    //  cell.backgroundColor = [UIColor redColor];
    
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 3) {
        
        cell.chakanLabel.hidden = NO;
        cell.neirongtextview.hidden = YES;
        
        
    } else {
        cell.chakanLabel.hidden = YES;
        cell.neirongtextview.hidden = NO ;
        
    }
    
    return  cell;
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HWpinglunFrame *frame =     self.dataMuArr[indexPath.section];
    HWpinglunxiangqingshituView *pinglun = [[HWpinglunxiangqingshituView alloc]init];
    pinglun.idStr =frame.datamodel.id;
    [[HWViewUtils getCurrentVC].navigationController pushViewController:pinglun animated:YES];
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (indexPath.row == 3) {
        return  20;
    }
    
    HWpinglunFrame *frame =     self.dataMuArr[indexPath.section];
    HWpinglunModel *model =      frame.datamodel.son_list[indexPath.row];
    
    
    NSString  *content = [NSString stringWithFormat:@"%@:%@",model.user_name, model.content];
    
    
    
    if (model.beihuifu_user_name == nil  ) {
        
        content = [NSString stringWithFormat:@"%@:%@",model.user_name, model.content];
        
        
    } else {
        
        content = [NSString stringWithFormat:@"%@回复%@:%@",model.user_name,model.beihuifu_user_name ,model.content];
        
    }
    
    //   content = @"四川成家长把孩子送进校园后，纷纷贴墙“暗中观察”。9月1日，全国多地迎来都一小学开学，家长把孩子送进校后，纷纷贴墙“暗中";
    
    CGSize   size2 =    [WMUtil sizeWithFont1:[UIFont systemFontOfSize:14] maxSize:CGSizeMake(kScreenW-60-15-20, 2000) string:content];
    CGFloat a=  8;
    //   self.neirongtextview.text = content;
    if (size2.height < 17) {
        return   size2.height+a ;
        
    } else {
        // a=  -10;
        return   size2.height+a;
        
    }
    
    
    return  150;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    
    HWpinglunFrame *frame =     self.dataMuArr[section];
    return   frame.sectionHeight;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return  36;
    
    
    
    
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    
    HWpinglunHeaderView *view = [HWpinglunHeaderView headFooterViewWithTableview:tableView];
    // view.frame = CGRectMake(0, 0, kScreenW, 100);
    view.model = self.dataMuArr[section];
    view.delegate = self;
    view.backgroundColor = [UIColor redColor];
    return view;
    
    
    
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    
    HWpinglunfootView *view = [HWpinglunfootView headFooterViewWithTableview:tableView];
    // view.frame = CGRectMake(0, 0, kScreenW, 100);
    view.model = self.dataMuArr[section];
    view.section = section;
    view.delegate = self;
    view.backgroundColor = [UIColor redColor];
    
    return  view;
    
    
    
    
}





- (void)HWpinglunHeaderView:(HWpinglunFrame*)frame {
    
    
    HWpinglunxiangqingshituView *pilunxiang = [[HWpinglunxiangqingshituView alloc] init];
    pilunxiang.pid = frame.datamodel.id;
    pilunxiang.gidStr = frame.datamodel.gid;
    pilunxiang.idStr = frame.datamodel.id;
    [[HWViewUtils getCurrentVC].navigationController  pushViewController:pilunxiang animated:YES];
    
    
    
    
}



- (void)HWpinglunfootView:(HWpinglunFrame *)modeFrame sectionInt:(NSInteger)section {
    
    
    
    if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
        
        HWLoginController *login = [[HWLoginController alloc]init];
        [[HWViewUtils getCurrentVC].navigationController  pushViewController:login animated:YES];
        
        
        return ;
        
    }
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t = [WMUtil getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
    NSString *signMD5 = [WMUtil md5HexDigest:sign];
    
    NSDictionary *parametes = @{
        @"ct" : @"app2021",
        @"ac" : @"pinglun_zan",
        @"ios" : @"1",
        @"uid" :userID,
        @"t" :tStr,
        @"sign":signMD5,
        @"id":modeFrame.datamodel.id
    };
    
    
    //  @"images" :ico,
    
    [MBProgressHUD showMessage:@"正在点赞..."];
    [HWWebUtils doPostWithParameters:parametes progress:nil success:^(id response) {
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
            // [self.navigationController popViewControllerAnimated:YES];
            //    [MBProgressHUD  showError:@"发表评论成功"];
            [self detailsInterfaceCommentsxiangqing];
            
            
        } else {
            
            [MBProgressHUD  showError:response[@"message"]];
            
        }
        NSLog(@"%@",response);
        
    } failure:^(NSString *error) {
        [MBProgressHUD  showError:@"加载失败"];
        
    }];
    
    
}



- (void)HWpinglunfootView:(HWpinglunFrame *)modeFrame {
    
    
    HWpinglunxiangqingshituView *pilunxiang = [[HWpinglunxiangqingshituView alloc] init];
    pilunxiang.pid = modeFrame.datamodel.id;
    pilunxiang.gidStr = modeFrame.datamodel.gid;
    pilunxiang.idStr = modeFrame.datamodel.id;
    [[HWViewUtils getCurrentVC].navigationController  pushViewController:pilunxiang animated:YES];
    
    
    
    
    
}




- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollCallback(scrollView);
    
}


#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return self.maintablview;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self;
}

@end
